﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess.SqlServer
{
    public class SqlServerHopDongLamViec: IHopDongLamViec
    {
        public HopDongLamViec GetHopDongLamViec(int oid)
        {
            string sql = @" Select  a.SoHopDong, a.NgayKy, a.ChucVuNguoiKy, a.NguoiKy, a.NguoiLaoDong, a.DiaDiemLamViec, a.ChucDanhChuyenMon, a.LoaiHopDong,
                                    b.QuanLyHopDong, b.HinhThucHopDong, b.TuNgay, b.DenNgay,
                                    c.LoaiHopDong, c.DieuKhoanHopDong
                            From HopDong a inner join HopDongNhanVien b on a.Oid = b.Oid
                                            inner join HopDongLamViec c on b.Oid = c.Oid
                            Where a.Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            HopDongLamViec hopDongLamViec = new HopDongLamViec();
            hopDongLamViec.Oid = oid;
            hopDongLamViec.SoHopDong = row["SoHopDong"].ToString();
            hopDongLamViec.NgayKy = DateTime.Parse(row["NgayKy"].ToString());
            int id;
            if (int.TryParse(row["ChucVuNguoiKy"].ToString(), out id))
            {
                SqlServerChucVu sqlServerChucVu = new SqlServerChucVu();
                hopDongLamViec.ChucVuNguoiKy = sqlServerChucVu.GetChucVu(id);
            }
            if (int.TryParse(row["NguoiKy"].ToString(), out id))
            {
                SqlServerNhanVien sqlServerNhanVien = new SqlServerNhanVien();
                hopDongLamViec.NguoiKy = sqlServerNhanVien.GetNhanVien(id);
            }
            if (int.TryParse(row["NguoiLaoDong"].ToString(), out id))
            {
                //SqlServerHoSo sqlServerHoSo = new SqlServerHoSo();
                //hopDongLamViec.NguoiLaoDong = sqlServerHoSo.GetHoSo(id);
                hopDongLamViec.NguoiLaoDong = HoSoFactory.GetHoSo(id);

            }
            if (int.TryParse(row["DiaDiemLamViec"].ToString(), out id))
            {
                SqlServerBoPhan sqlServerBoPhan = new SqlServerBoPhan();
                hopDongLamViec.DiaDiemLamViec = sqlServerBoPhan.GetBoPhan(id);
            }
            hopDongLamViec.ChucDanhChuyenMon = row["ChucDanhChuyenMon"].ToString();


            byte loaiHopDongLamViec;
            if (byte.TryParse(row["LoaiHopDong"].ToString(), out loaiHopDongLamViec))
            {
                hopDongLamViec.PhanLoai = (ELoaiHopDongLamViec)loaiHopDongLamViec;
            }
            if (int.TryParse(row["DieuKhoanHopDong"].ToString(), out id))
            {
                SqlServerDieuKhoanHopDong sqlServerDieuKhoanHopDong = new SqlServerDieuKhoanHopDong();
                hopDongLamViec.DieuKhoanHopDong = sqlServerDieuKhoanHopDong.GetDieuKhoanHopDong(id);
            }
            return hopDongLamViec;
        }

        public int Insert(HopDongLamViec hopDongLamViec)
        {
            SqlServerHopDongNhanVien sqlServerHopDongNhanVien = new SqlServerHopDongNhanVien();
            int oid = sqlServerHopDongNhanVien.Insert((HopDongNhanVien)hopDongLamViec);

            if (oid > 0)
            {
                string sql = @" Insert into HopDongLamViec(Oid, LoaiHopDong, DieuKhoanHopDong)
                            Values(@Oid, @LoaiHopDong, @DieuKhoanHopDong)";

                SqlParameter[] param = {
                                       new SqlParameter("@Oid", oid),
                                       new SqlParameter("@LoaiHopDong", hopDongLamViec.PhanLoai),
                                       new SqlParameter("@DieuKhoanHopDong", hopDongLamViec.DieuKhoanHopDong.Oid)
                                   };
                int temp = DataProvider.Insert(sql, false, param);

                if (oid > 0)
                {
                    ThamChieuDuLieu thamChieuDuLieu = new ThamChieuDuLieu();
                    thamChieuDuLieu.Cha = new SqlServerKieuDuLieu().GetKieuDuLieu(typeof(HopDong).ToString());
                    thamChieuDuLieu.Con = new SqlServerKieuDuLieu().GetKieuDuLieu(typeof(HopDongLamViec).ToString());
                    thamChieuDuLieu.GiaTri = oid;

                    SqlServerThamChieuDuLieu sqlServerThamChieuDuLieu = new SqlServerThamChieuDuLieu();
                    temp = sqlServerThamChieuDuLieu.Insert(thamChieuDuLieu);
                }
            }
            return oid;
        }

        public int Update(HopDongLamViec hopDongLamViec)
        {
            SqlServerHopDongNhanVien sqlServerHopDongNhanVien = new SqlServerHopDongNhanVien();
            int temp = sqlServerHopDongNhanVien.Update((HopDongNhanVien)hopDongLamViec);

            string sql = @" Update HopDongLaoDong
                            Set 
                                LoaiHopDong = @LoaiHopDong, 
                                DieuKhoanHopDong = @DieuKhoanHopDong
                            Where Oid = @Oid";

            SqlParameter[] param = {
                                       new SqlParameter("@Oid", hopDongLamViec.Oid),
                                       new SqlParameter("@LoaiHopDong", hopDongLamViec.PhanLoai),
                                       new SqlParameter("@DieuKhoanHopDong", hopDongLamViec.DieuKhoanHopDong.Oid)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(HopDongLamViec hopDongLamViec)
        {
            string sql = @" Delete From HopDongLaoDong Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", hopDongLamViec.Oid);
            int temp = DataProvider.Update(sql, param);

            SqlServerHopDongNhanVien sqlServerHopDongNhanVien = new SqlServerHopDongNhanVien();
            temp = sqlServerHopDongNhanVien.Delete((HopDongNhanVien)hopDongLamViec);
            return temp;
        }


        public int Save(HopDongLamViec obj)
        {
            throw new NotImplementedException();
        }
    }
}
